<template>
  <div/>

</template>

<script>
export default {
  name: 'BaseEchartPie',
  components: { },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    charDivId: {
      type: String,
      required: true
    },
    // divWidth: {
    //   type: String,
    //   default: '500px'
    // },
    // divHeigth: {
    //   type: String,
    //   default: '400px'
    // },
    title: {
      type: String,
      default: ''
    },
    subTitle: {
      type: String,
      default: ''
    },
    seriesName: {
      type: String,
      default: ''
    },
    datas: Array
  },
  data() {
    return {
      myChart: {},
      chartdata: {
        legendData: [],
        seriesData: [],
        selected: {}
      },
      option: {
        title: {
          text: this.title,
          subtext: this.subTitle,
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          // type: 'scroll',
          // orient: 'vertical',
          // right: 10,
          // top: 20,
          // bottom: 20
          bottom: 10,
          left: 'center'
          // data: this.chartdata.legendData,
          // selected: this.chartdata.selected
        },
        series: [
          {
            name: this.seriesName,
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            // data: this.chartdata.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    }
  },
  watch: {
    'datas': function() {
      this.draw()
    }
  },
  mounted: function() {
    this.init()
  },
  methods: {
    initChar(gas, water) {
      console.log(gas)
      console.log(water)
      // 基于准备好的dom，初始化echarts实例
      this.myChart = this.$echarts.init(document.getElementById(this.charDivId))
    },
    draw() {
      const chartArray = this.datas
      if (chartArray && chartArray instanceof Array && chartArray.length > 0) {
        this.chartdata = {
          legendData: [],
          seriesData: [],
          selected: {}
        }

        let tempName
        for (let i = 0; i < chartArray.length; i++) {
          tempName = chartArray[i].name
          if (tempName) { // name为空则该项不显示
            this.chartdata.legendData.push(tempName)
            this.chartdata.seriesData.push({
              name: tempName,
              value: chartArray[i].value
            })
            this.chartdata.selected[tempName] = chartArray[i].value > 0
          }
        }
        this.option.legend.data = this.chartdata.legendData
        this.option.legend.selected = this.chartdata.selected
        this.option.series[0].data = this.chartdata.seriesData
        this.myChart.setOption(this.option, true)
      }
    },
    init() {
      this.initChar()
      // this.draw()
    }
  }
}
</script>

<style scoped>

.cardRadius {
  border-radius: 15px;
}
.verticalDivider {
  margin-top: 5%;
  margin-bottom: 5%;
  border: 2px solid #e6ebef;
}
div {
  margin: 0px;
  padding: 0px;
}
</style>
